<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/9/5
 * Time: 下午4:31
 */

namespace app\admin\controller;


use think\Config;
use think\Image;
use think\Request;

class Member extends  Common
{
    public  function index ()
    {
        $search = input('get.search' , '');
        if ($search) {
            $adlist = db('member')->where('real_name' ,'like', '%'.$search.'%')->paginate(15);

        }else{
            $adlist = db('member')->paginate(15);

        }
        $page = $adlist ->render();
        $page = str_replace([
            'class="pagination"',
            'class="disabled"',
            'class="active"',
        ] , [
            'class="am-pagination tpl-pagination"',
            'class="am-disabled"',
            'class="am-active"',
        ] , $page);
        if (Request::instance()->isAjax()) {
            $data = json(['adlist'=>$adlist , 'page'=>$page]);
            return $data;
        }else {
            $this->assign('adlist' , $adlist);
            $this->assign('page' , $page);
            return view();
        }
    }

    public function create ()
    {
        return view();
    }

    public function store ()
    {

        if (Request::instance()->isAjax()) {
            $file = \request()->file('src');
            if ($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
//                     成功上传后 获取上传信息
//                     输出 jpg
                    $msg['src'] = Config::get('view_replace_str')['__UPLOAD__'].$info->getSaveName();
                    $msg['savename'] = $info->getSaveName();
                    $msg['status'] = 1;

                    $image = Image::open('.'.$msg['src']);
                    $thumb_path = $info->getPath() . '_thumb';
                    file_exists($thumb_path)  || mkdir($thumb_path , 0777 , true);
//                    echo $info->getPath();die;

                    $image->thumb(150, 150)->save($thumb_path .'/'. $info->getBasename());

                }else{
                    // 上传失败获取错误信息
                    $msg['msg'] = $file->getError();
                    $msg['status'] = 0;
                }
                return json($msg);
            }
        }else {
            $info = input('post.');
            $info['src'] = input('post.src2');
            unset($info['src2']);
            db('ad')->insert($info);
            $this->success('添加轮播图成功', url('index'));
        }
    }
}